const adminModel = require('../models/adminModel');
const utils = require('../utils/index');
const myGlobal = require('path')
    //--------------娴后台轮播图
exports.getAllBanner = function(req, res) {
        myGlobal.path = req.originalUrl;
        adminModel.getAllBanner().then(function(data) {
            // console.log('=================================');
            // console.log(data);
            data = utils.arrFormat(data)
            res.render('admin/banner', {
                // user: req.session.user,
                path: myGlobal.path,
                banners: data,
                user:req.session.user
            })
        })
    }
    //娴添加后台轮播
exports.addBannerPage = function(req, res) {
    res.render('admin/banner_add',{path:myGlobal.path,user:req.session.user})
}
exports.addBanner = function(req, res) {
    req.body.imgName = req.file.filename;
    adminModel.addBanner(req.body).then(function(data) {
        if (data.affectedRows > 0) {
            res.redirect('/admin/banner')
        }
    })

}
exports.index = (req, res) => {
    myGlobal.path = req.originalUrl;
    adminModel.getNewArtical().then(function(data) {
        // console.log(data)
        data = utils.arrFormat(data);
        res.render('admin/home', { user: req.session.user, newLists: data ,path:myGlobal.path})
    })

}

exports.loginPage = function(req, res) {
    res.render('admin/adLogin')
}

exports.login = function(req, res) {
    adminModel.login(req.body).then(function(data) {
        if (data.length > 0) {
            req.session.user = data[0]
            res.redirect('/admin')
        } else {
            res.render('admin/adLogin', { errMsg: '用户名密码错误' })
        }
    })
}

//娴修改后台轮播图
exports.getOneBanner = function(req, res) {
    console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
    // console.log(req.params.id);

    adminModel.getOneBanner(req.params.id).then(function(data) {
        // console.log(data);
        //点击编辑按钮时,当前轮播图图片名称保存
        if (data.length > 0) {
            myGlobal.oldImgName = data[0].imgName;
            res.render('admin/banner_edit', { path: myGlobal.path, oneBanner: data[0] })
        }

    })
}

exports.editBanner = function(req, res) {
    // console.log(123333333333);

    // console.log(req.body);

    //如果图片有值,就将图片值传到数据层
    if (req.file) {
        req.body.imgName = req.file.filename;
        console.log(myGlobal.oldImgName);
        if (myGlobal.oldImgName) {
            //同时删除旧的图片文件
            utils.deleteFile(myGlobal.oldImgName);
        }
    }
    adminModel.editBanner(req.body).then(function(data) {

        if (data.affectedRows > 0) {
            res.redirect('/admin/banner')
        }
    })
}

exports.deleteBanner = function(req, res) {
    utils.deleteFile(req.query.imgName);
    adminModel.deleteBanner(req.params.id).then(function(data) {
        if (data.affectedRows > 0) {
            res.redirect('/admin/banner')
        }
    })
}

//-----------------------------

exports.loginout = function(req, res) {
    req.session.user = null;
    res.redirect('/admin/login')
}



    // --------------------------------文章管理------------------------
exports.getAllArtical = (req, res) => {
    myGlobal.path = req.originalUrl;
    adminModel.getAllArtical(req.query.page).then((data) => {
        let result = {
            total: data[1][0].total,
            articals: data[0],
            path: myGlobal.path,
            user:req.session.user
        }
        res.render('admin/artical', result);

    })
}
exports.addArticalPage = (req, res) => {
    adminModel.addArticalPage().then((data) => {
        res.render('admin/artical_add', { path: myGlobal.path, types: data })
    })
}
exports.addArtical = (req, res) => {
        req.body.userId = 2;
        console.log(req.body);
        adminModel.addArtical(req.body).then((data) => {
            // console.log(data);

            if (data.affectedRows > 0) {
                res.redirect('/admin/artical')
            }
        })
    }
    //--------------------------用户管理模块-----------------------
exports.listUser = (req, res) => {
    myGlobal.path = req.originalUrl;

    adminModel.getAllUser(req.query.page).then(function(data) {

        let total = data[1][0].total
        data[0] = utils.arrFormat(data[0]);
        res.render('admin/user', { users: data[0], total: total,path:myGlobal.path,user:req.session.user })
    })
}

//新增：第一步
exports.addPage = (req, res) => {
        res.render('admin/user_add',{path:myGlobal.path,user:req.session.user})
    }
    //新增：第二步
exports.addUser = (req, res) => {
    adminModel.addUser(req.body).then(function(data) {
        if (data.affectedRows > 0) {
            res.redirect('/admin/user')
        }
    })
}

exports.deleteArtical = (req, res) => {
    adminModel.deleteArtical(req.params.id).then((data) => {
        if (data.affectedRows > 0) {
            res.redirect('/admin/artical')
        }
    })
}
exports.editArticalPage = (req, res) => {
    // res.render('admin/artical_edit',{path:myGlobal.path})
    adminModel.editArticalPage(req.params.id).then((data) => {
        // console.log(data);
        res.render('admin/artical_edit', { path: myGlobal.path, artical: data[0][0], types: data[1] ,user:req.session.user})
            // res.send(data[1]);
    })
}
exports.editArtical = (req, res) => {
        console.log(req.body);

        adminModel.editArtical(req.body).then((data) => {

            console.log(data);

            if (data.affectedRows > 0) {
                res.redirect('/admin/artical')
            }
        })
    }
    //编辑第一步
exports.getOneUser = (req, res) => {
    adminModel.getOneUser(req.params.id).then(function(data) {
        // console.log(data);

        res.render('admin/user_edit', { editUser: data[0] ,path:myGlobal.path,user:req.session.user})
    })

}

//编辑第二步
exports.editUser = (req, res) => {
    adminModel.editUser(req.body).then(function(data) {
        res.redirect('/admin/user')
    })
}

//删除
exports.deleteUser = (req, res) => {
        adminModel.deleteUser(req.params.id).then(function(data) {
            res.redirect('/admin/user')
        })
    }
    //查询
exports.searchUser = (req, res) => {
        if (req.body.search.length == 0) return res.json({ result: '空数据' })
        adminModel.searchUser(req.body.search).then(function(data) {

            res.json(data)
        })
    }
    // ---------------------菜单管理----------------
exports.menu = (req, res) => {
    myGlobal.path = req.originalUrl;
    adminModel.getAllMenu().then(data => {
        res.render('admin/menu', { menus: utils.arrFormat(data) ,path:myGlobal.path,user:req.session.user});
    })
}
exports.menuAdd = (req, res) => {
    res.render('admin/menu_add',{path:myGlobal.path,user:req.session.user});
}
exports.menuAddUser = (req, res) => {
    adminModel.AddMenu(req.body).then(data => {
        if (data.affectedRows > 0) {
            res.redirect('/admin/menu')
        }

    })

}
exports.menuEdit = (req, res) => {
    if (req.params.id == 'favicon.ico') return;
    console.log(req.params.id);
    adminModel.getOneMenu(req.params.id).then(data => {
        console.log(data);
        res.render('admin/menu_Edit', { menu: data[0] ,path:myGlobal.path,user:req.session.user})

    })

}
exports.menuEditUser = (req, res) => {
    // console.log(req.body);
    adminModel.editMenu(req.body).then(data => {
        if (data.affectedRows > 0) {
            res.redirect('/admin/menu')
        }
    })

}
exports.menuDelete = (req, res) => {
        // console.log(req.body);
        if (req.params.id == 'favicon.ico') return;
        console.log(req.params.id);

        adminModel.deleteMenu(req.params.id).then(data => {
            if (data.affectedRows > 0) {
                res.redirect('/admin/menu')
            }
        })

    }
    // ----------------文章分类模块---------------
    // router.get('/classify',adminController.classify);
    // router.get('/classifyAdd',adminController.classifyAdd);
    // router.post('/classifyAdd',adminController.classifyAddUser);
    // router.get('/classifyEdit/:id',adminController.classifyEdit);
    // router.post('/classifyEdit',adminController.classifyEditUser);
    // router.get('classifyDelete/:id',adminController.classifyDelete );
exports.classify = (req, res) => {
    myGlobal.path = req.originalUrl;
    adminModel.getAllClasses().then(data => {
        if (data.length > 0) {
            res.render('admin/class', { classes: utils.arrFormat(data),path:myGlobal.path ,user:req.session.user})
        }
    })
}
exports.classifyAdd = (req, res) => {
    res.render('admin/class_add',{path:myGlobal.path,user:req.session.user});
}
exports.classifyAddUser = (req, res) => {
    console.log(req.body);
    adminModel.AddClass(req.body).then(data => {
        if (data.affectedRows > 0) {
            res.redirect('/admin/classify')
        }
    })

}
exports.classifyEdit = (req, res) => {
    if (req.params.id == 'favicon.ico') return;
    adminModel.getOneClass(req.params.id).then(data => {
        console.log(data);
        res.render('admin/class_edit', { aclass: data[0],path:myGlobal.path,user:req.session.user })
    })
}
exports.classifyEditUser = (req, res) => {
    console.log(req.body);
    adminModel.editClass(req.body).then(data => {
        if (data.affectedRows > 0) {
            res.redirect('/admin/classify');
        }
    })


}
exports.classifyDelete = (req, res) => {
    if (req.params.id == 'favicon.ico') return;
    console.log(req.params.id);

    adminModel.deleteClass(req.params.id).then(data => {
        if (data.affectedRows > 0) {
            res.redirect('/admin/classify');
        }
    })


}